home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 June / SGI Freeware 1998 June.iso / dist / fw_gawk.idb / usr / freeware / bin / igawk.z / igawk
Text File  |  1998-05-21  |  3KB  |  131 lines

  1. #! /bin/sh
  2.  
  3. # igawk --- like gawk but do @include processing
  4. # Arnold Robbins, arnold@gnu.ai.mit.edu, Public Domain
  5. # July 1993
  6.  
  7. if [ "$1" = debug ]
  8. then
  9.     set -x
  10.     shift
  11. else
  12.     # cleanup on exit, hangup, interrupt, quit, termination
  13.     trap 'rm -f /tmp/ig.[se].$$' 0 1 2 3 15
  14. fi
  15.  
  16. while [ $# -ne 0 ] # loop over arguments
  17. do
  18.     case $1 in
  19.     --)     shift; break;;
  20.  
  21.     -W)     shift
  22.             set -- -W"$@"
  23.             continue;;
  24.  
  25.     -[vF])  opts="$opts $1 '$2'"
  26.             shift;;
  27.  
  28.     -[vF]*) opts="$opts '$1'" ;;
  29.  
  30.     -f)     echo @include "$2" >> /tmp/ig.s.$$
  31.             shift;;
  32.  
  33.     -f*)    f=`echo "$1" | sed 's/-f//'`
  34.             echo @include "$f" >> /tmp/ig.s.$$ ;;
  35.  
  36.     -?file=*)    # -Wfile or --file
  37.             f=`echo "$1" | sed 's/-.file=//'`
  38.             echo @include "$f" >> /tmp/ig.s.$$ ;;
  39.  
  40.     -?file)    # get arg, $2
  41.             echo @include "$2" >> /tmp/ig.s.$$
  42.             shift;;
  43.  
  44.     -?source=*)    # -Wsource or --source
  45.             t=`echo "$1" | sed 's/-.source=//'`
  46.             echo "$t" >> /tmp/ig.s.$$ ;;
  47.  
  48.     -?source)  # get arg, $2
  49.             echo "$2" >> /tmp/ig.s.$$
  50.             shift;;
  51.  
  52.     -?version)
  53.             echo igawk: version 1.0 1>&2
  54.             gawk --version
  55.             exit 0 ;;
  56.  
  57.     -[W-]*)    opts="$opts '$1'" ;;
  58.  
  59.     *)      break;;
  60.     esac
  61.     shift
  62. done
  63.  
  64. if [ ! -s /tmp/ig.s.$$ ]
  65. then
  66.     if [ -z "$1" ]
  67.     then
  68.          echo igawk: no program! 1>&2
  69.          exit 1
  70.     else
  71.         echo "$1" > /tmp/ig.s.$$
  72.         shift
  73.     fi
  74. fi
  75.  
  76. # at this point, /tmp/ig.s.$$ has the program
  77. gawk -- '
  78. # process @include directives
  79.  
  80. function pathto(file,    i, t, junk)
  81. {
  82.     if (index(file, "/") != 0)
  83.         return file
  84.  
  85.     for (i = 1; i <= ndirs; i++) {
  86.         t = (pathlist[i] "/" file)
  87.         if ((getline junk < t) > 0) {
  88.             # found it
  89.             close(t)
  90.             return t
  91.         }
  92.     }
  93.     return ""
  94. }
  95. BEGIN {
  96.     path = ENVIRON["AWKPATH"]
  97.     ndirs = split(path, pathlist, ":")
  98.     for (i = 1; i <= ndirs; i++) {
  99.         if (pathlist[i] == "")
  100.             pathlist[i] = "."
  101.     }
  102.     stackptr = 0
  103.     input[stackptr] = ARGV[1] # ARGV[1] is first file
  104.  
  105.     for (; stackptr >= 0; stackptr--) {
  106.         while ((getline < input[stackptr]) > 0) {
  107.             if (tolower($1) != "@include") {
  108.                 print
  109.                 continue
  110.             }
  111.             fpath = pathto($2)
  112.             if (fpath == "") {
  113.                 printf("igawk:%s:%d: cannot find %s\n", \
  114.                     input[stackptr], FNR, $2) > "/dev/stderr"
  115.                 continue
  116.             }
  117.             if (! (fpath in processed)) {
  118.                 processed[fpath] = input[stackptr]
  119.                 input[++stackptr] = fpath
  120.             } else
  121.                 print $2, "included in", input[stackptr], \
  122.                     "already included in", \
  123.                     processed[fpath] > "/dev/stderr"
  124.         }
  125.         close(input[stackptr])
  126.     }
  127. }' /tmp/ig.s.$$ > /tmp/ig.e.$$
  128. eval gawk -f /tmp/ig.e.$$ $opts -- "$@"
  129.  
  130. exit $?
  131.